package common_error

import (
	"gitee.com/xlizy/common-go/base/enums"
	"strconv"
)

type CommonError int

const (
	SystemError                CommonError = 1000
	SystemBusy                 CommonError = 1001
	FrequentOperations         CommonError = 1002
	TooManyMistakes            CommonError = 1003
	VerCodeError               CommonError = 1004
	VerCodeNotExist            CommonError = 1005
	VerCodeExpired             CommonError = 1006
	SmartVerificationError     CommonError = 1007
	ParameterMissing           CommonError = 1008
	VerCodeIsUsed              CommonError = 1009
	RequestArgumentNotValid    CommonError = 1010
	RequestArgumentParserError CommonError = 1011
	RequestArgumentMissing     CommonError = 1012
	DuplicateKey               CommonError = -1013
	UploadFileSizeLimit        CommonError = 1014
	UploadFileError            CommonError = 1015
	CheckDataSignError         CommonError = 1016
	DecryptDataError           CommonError = 1017
	DubboServiceUnavailable    CommonError = 1018
	UrlNotFound                CommonError = 1019
	NotLoggedIn                CommonError = 1020
	SysErrEnumError            CommonError = -1021
	AccessDenied               CommonError = 1022
	CallCaptchaError           CommonError = 1023
	HttpCallError              CommonError = 1024
	HttpCallTimeout            CommonError = 1025
	RpcCallError               CommonError = 1026
	RpcExecuteError            CommonError = 1027
	MysqlErr                   CommonError = 1029
	MysqlErr1008               CommonError = 1030
	MysqlErr1012               CommonError = 1031
	MysqlErr1020               CommonError = 1032
	MysqlErr1021               CommonError = 1033
	MysqlErr1022               CommonError = 1034
	MysqlErr1037               CommonError = 1035
	MysqlErr1044               CommonError = 1036
	MysqlErr1045               CommonError = 1037
	MysqlErr1048               CommonError = 1038
	MysqlErr1049               CommonError = 1039
	MysqlErr1054               CommonError = 1040
	MysqlErr1062               CommonError = 1041
	MysqlErr1065               CommonError = 1042
	MysqlErr1114               CommonError = 1043
	MysqlErr1130               CommonError = 1044
	MysqlErr1133               CommonError = 1045
	MysqlErr1141               CommonError = 1046
	MysqlErr1142               CommonError = 1047
	MysqlErr1143               CommonError = 1048
	MysqlErr1149               CommonError = 1049
	MysqlErr1146               CommonError = 1146
	MysqlErr1169               CommonError = 1051
	MysqlErr1216               CommonError = 1052
	InsufficientScope          CommonError = 1060
	DicItemNotExist            CommonError = 1100
	RowsAffectedZero           CommonError = 1101
	DataNotExist               CommonError = 1102
	Default                    CommonError = -1
)

func (e CommonError) Code() int32 {
	return int32(e)
}

func (e CommonError) Des() string {
	return enums.BE(e).Des
}

func (e CommonError) Enum() string {
	return enums.BE(e).Enum
}

func (e CommonError) Val() int {
	return int(e)
}

func (e *CommonError) UnmarshalJSON(value []byte) error {
	*e = CommonError(enums.UnmarshalEnum(value))
	return nil
}

func (e CommonError) MarshalJSON() ([]byte, error) {
	switch e {
	case SystemError:
		return enums.JsonObj(int(e), "SystemError", "通用错误")
	case SystemBusy:
		return enums.JsonObj(int(e), "SystemBusy", "系统繁忙,请稍候再试")
	case FrequentOperations:
		return enums.JsonObj(int(e), "FrequentOperations", "操作频繁,请稍后再试")
	case TooManyMistakes:
		return enums.JsonObj(int(e), "TooManyMistakes", "错误尝试过多,请稍后再试")
	case VerCodeError:
		return enums.JsonObj(int(e), "VerCodeError", "验证码错误")
	case VerCodeNotExist:
		return enums.JsonObj(int(e), "VerCodeNotExist", "验证码不存在")
	case VerCodeExpired:
		return enums.JsonObj(int(e), "VerCodeExpired", "验证码已过期")
	case SmartVerificationError:
		return enums.JsonObj(int(e), "SmartVerificationError", "行为验证失败")
	case ParameterMissing:
		return enums.JsonObj(int(e), "ParameterMissing", "参数缺失")
	case VerCodeIsUsed:
		return enums.JsonObj(int(e), "VerCodeIsUsed", "验证码已使用")
	case RequestArgumentNotValid:
		return enums.JsonObj(int(e), "RequestArgumentNotValid", "请求参数校验不通过")
	case RequestArgumentParserError:
		return enums.JsonObj(int(e), "RequestArgumentParserError", "请求参数解析失败")
	case RequestArgumentMissing:
		return enums.JsonObj(int(e), "RequestArgumentMissing", "请求参数缺失")
	case DuplicateKey:
		return enums.JsonObj(int(e), "DuplicateKey", "主键或唯一键约束失败")
	case UploadFileSizeLimit:
		return enums.JsonObj(int(e), "UploadFileSizeLimit", "上传文件大小超过限制")
	case UploadFileError:
		return enums.JsonObj(int(e), "UploadFileError", "上传文件错误")
	case CheckDataSignError:
		return enums.JsonObj(int(e), "CheckDataSignError", "数据验签失败")
	case DecryptDataError:
		return enums.JsonObj(int(e), "DecryptDataError", "数据解密失败")
	case DubboServiceUnavailable:
		return enums.JsonObj(int(e), "DubboServiceUnavailable", "系统繁忙,请稍候再试")
	case UrlNotFound:
		return enums.JsonObj(int(e), "UrlNotFound", "未匹配到请求URL")
	case NotLoggedIn:
		return enums.JsonObj(int(e), "NotLoggedIn", "登录状态异常,请重新登录后操作")
	case SysErrEnumError:
		return enums.JsonObj(int(e), "SysErrEnumError", "response枚举转换错误")
	case AccessDenied:
		return enums.JsonObj(int(e), "AccessDenied", "拒绝访问")
	case CallCaptchaError:
		return enums.JsonObj(int(e), "CallCaptchaError", "调用验证码服务异常")
	case HttpCallError:
		return enums.JsonObj(int(e), "HttpCallError", "http请求失败")
	case HttpCallTimeout:
		return enums.JsonObj(int(e), "HttpCallTimeout", "http请求超时")
	case RpcCallError:
		return enums.JsonObj(int(e), "RpcCallError", "rpc请求失败")
	case RpcExecuteError:
		return enums.JsonObj(int(e), "RpcExecuteError", "rpc服务端发生异常")
	case MysqlErr:
		return enums.JsonObj(int(e), "MysqlErr", "Sql执行异常")
	case MysqlErr1008:
		return enums.JsonObj(int(e), "MysqlErr1008", "数据库不存在")
	case MysqlErr1012:
		return enums.JsonObj(int(e), "MysqlErr1012", "不能读取系统表中的记录")
	case MysqlErr1020:
		return enums.JsonObj(int(e), "MysqlErr1020", "记录已被其他用户修改")
	case MysqlErr1021:
		return enums.JsonObj(int(e), "MysqlErr1021", "硬盘剩余空间不足，请加大硬盘可用空间")
	case MysqlErr1022:
		return enums.JsonObj(int(e), "MysqlErr1022", "关键字重复，更改记录失败")
	case MysqlErr1037:
		return enums.JsonObj(int(e), "MysqlErr1037", "系统内存不足，请重启数据库或重启服务器")
	case MysqlErr1044:
		return enums.JsonObj(int(e), "MysqlErr1044", "当前用户没有访问数据库的权限")
	case MysqlErr1045:
		return enums.JsonObj(int(e), "MysqlErr1045", "不能连接数据库，用户名或密码错误")
	case MysqlErr1048:
		return enums.JsonObj(int(e), "MysqlErr1048", "字段不能为空")
	case MysqlErr1049:
		return enums.JsonObj(int(e), "MysqlErr1049", "数据库不存在")
	case MysqlErr1054:
		return enums.JsonObj(int(e), "MysqlErr1054", "字段不存在")
	case MysqlErr1062:
		return enums.JsonObj(int(e), "MysqlErr1062", "违反唯一性约束")
	case MysqlErr1065:
		return enums.JsonObj(int(e), "MysqlErr1065", "无效的SQL语句，SQL语句为空")
	case MysqlErr1114:
		return enums.JsonObj(int(e), "MysqlErr1114", "数据表已满，不能容纳任何记录")
	case MysqlErr1130:
		return enums.JsonObj(int(e), "MysqlErr1130", "连接数据库失败，没有连接数据库的权限")
	case MysqlErr1133:
		return enums.JsonObj(int(e), "MysqlErr1133", "数据库用户不存在")
	case MysqlErr1141:
		return enums.JsonObj(int(e), "MysqlErr1141", "当前用户无权访问数据库")
	case MysqlErr1142:
		return enums.JsonObj(int(e), "MysqlErr1142", "当前用户无权访问数据表")
	case MysqlErr1143:
		return enums.JsonObj(int(e), "MysqlErr1143", "当前用户无权访问数据表中的字段")
	case MysqlErr1146:
		return enums.JsonObj(int(e), "MysqlErr1146", "数据表不存在")
	case MysqlErr1149:
		return enums.JsonObj(int(e), "MysqlErr1149", "SQL语句语法错误")
	case MysqlErr1169:
		return enums.JsonObj(int(e), "MysqlErr1169", "字段值重复，更新记录失败")
	case MysqlErr1216:
		return enums.JsonObj(int(e), "MysqlErr1216", "外键约束检查失败，更新子表记录失败")
	case InsufficientScope:
		return enums.JsonObj(int(e), "InsufficientScope", "授权不足")
	case DicItemNotExist:
		return enums.JsonObj(int(e), "DicItemNotExist", "字典项不存在")
	case RowsAffectedZero:
		return enums.JsonObj(int(e), "RowsAffectedZero", "受影响行数为0")
	case DataNotExist:
		return enums.JsonObj(int(e), "DataNotExist", "数据不存在")

	}
	return []byte(strconv.Itoa(int(e))), nil
}
